import re
import csv
import time
import logging
from weixin_modules import oper_database
import settings
from weixin_modules.inner_settings import keywords_puyang_csvpath

logger = logging.getLogger(__name__)


def is_in_list(name_text, list_database):
    """
    判断 值 ，是否在列表中，
    :param value:
    :param vlist:
    :return: BOOL
    """
    if "@" in name_text:
        name_text_list = name_text.split("@")
        name_text = name_text_list[0]
        if len(name_text) < 1:
            name_text = name_text_list[1]
            if len(name_text) < 1:
                name_text = "@"
        name_text = name_text.strip()
    if list_database:
        for name_db in list_database:
            if name_text in name_db:
                # 跳过
                logger.debug(f"存在于列表中-->{name_text}")
                return True
        return False
    else:
        # 空列表就直接返回，
        return False


def deal_name_text(name_text):
    """
    处理 名字前后的空格 ，
    名字中间的 “ ‘
    等等，
    :param name_text:
    :return:
    """
    if len(name_text) > 40:
        logger.debug("超过50个字符，截取前50个字符")
        name_text = name_text[:40]
    name_text = name_text.strip()
    # 有些名字过长，会缩略，以 … 结束，
    if name_text.endswith("…"):
        name_text = name_text[:-1]
        name_text = name_text.strip()
    patterns = ["'", '"', "/", "<", ">"]
    for pattern in patterns:
        name_text = name_text.replace(pattern, "")
    name_text = name_text.strip()
    return name_text

def is_homepage(title_text):
    """
    判断是不是信息流界面
    例：微信(21)

    :return:
    """
    title_text = title_text.rstrip()
    if title_text == "微信":
        return True
    match = re.search("^微信\(\d+\)$", title_text)
    if match:
        return True
    else:
        return False


def is_rooms(title_text):
    """
    例：聊天信息(130) #
    使用正则判断是不是微信群，
    :param title_text:
    :return:
    """
    if title_text.endswith("#"):
        title_text = title_text[:-1]
    title_text = title_text.rstrip()
    match = re.search("\((\d{1,3})\)$", title_text)
    if match:
        return match.group(1)
    else:
        return False


def is_company_room(name_text):
    """
    # 通过标题 名称 判断是不是企业微信，
    :param name_text:
    :return:
    """
    # 3种方法可以判断 是不是企业微信，
    # te = b' \xc2\xa0'.decode()
    # te = chr(0xa0)
    te = "  "
    if name_text.endswith(te):
        logger.debug(f"企业微信:{name_text}")
        # 是企业微信 群
        return 1
    else:
        # 不是企业微信群
        return 0


def get_puyang_keys():
    """
    从csv文件 中获取 关于濮阳的 关键字，
    :return:
    """
    with open(keywords_puyang_csvpath, "r", encoding="utf-8") as f:
        reader = csv.reader(f)
        for row in reader:
            return row

    # def is_puyang(self, name_text):
    #     """
    #     判断名字中是否包含 关于濮阳的关键字，
    #     :param name_text:
    #     :return:
    #     """
    #     for key in puyang_keys:
    #         if key in name_text:
    #             return True
    #     return False




# def temp_re(room_name):
#     """
#     临时方法，放弃原来的群名称格式，
#     用正则的方式，匹配群备注名称
#     :param room_name:
#     :return:
#     """
#     result = re.match('.*🪺濮\d{9}', room_name)
#     if result:
#         print("匹配成功，匹配的内容是：" + result.group())
#         return True
#     else:
#         print("匹配失败")
#         return False


# 用64进制，占3位，
# 64进制 ___  对应的10进制：262143，，能够满足需求了，

def decimal_to_base64(decimal):
    """
    10进制数，转换为64进制
    :param decimal:
    :return:
    """
    base64_digits = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_"
    base64 = ""
    while decimal > 0:
        remainder = decimal % 64
        base64 = base64_digits[remainder] + base64
        decimal = decimal // 64

    nlen = len(base64)
    if nlen < 2:
        base64 = "00" + base64
        return base64
    if nlen < 3:
        base64 = "0" + base64
        return base64


def base64_to_decimal(base64_number):
    """
    64进制转换为 10进制
    :param base64_number:
    :return:
    """

    # 创建一个映射表，将每个字符映射到其10进制值
    base64_to_decimal_mapping = {
        '0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9,
        'a': 10, 'b': 11, 'c': 12, 'd': 13, 'e': 14, 'f': 15, 'g': 16, 'h': 17, 'i': 18, 'j': 19,
        'k': 20, 'l': 21, 'm': 22, 'n': 23, 'o': 24, 'p': 25, 'q': 26, 'r': 27, 's': 28, 't': 29,
        'u': 30, 'v': 31, 'w': 32, 'x': 33, 'y': 34, 'z': 35, 'A': 36, 'B': 37, 'C': 38, 'D': 39,
        'E': 40, 'F': 41, 'G': 42, 'H': 43, 'I': 44, 'J': 45, 'K': 46, 'L': 47, 'M': 48, 'N': 49,
        'O': 50, 'P': 51, 'Q': 52, 'R': 53, 'S': 54, 'T': 55, 'U': 56, 'V': 57, 'W': 58, 'X': 59,
        'Y': 60, 'Z': 61, '-': 62, '_': 63
    }

    decimal_number = 0
    power = len(base64_number) - 1

    for char in base64_number:
        decimal_number += base64_to_decimal_mapping[char] * (64 ** power)
        power -= 1

    return decimal_number


def dec_to_base32(n):
    convert_string = "0123456789ABCDEFGHIJKLMNOPQRSTUV"
    if n < 32:
        result = convert_string[n]
    else:
        result = ""
        while n > 0:
            n, remainder = divmod(n, 32)
            result = convert_string[remainder] + result
    return result


def base32_to_dec(s):
    return int(s, 32)



if __name__ == '__main__':
    title = "微信(21)"
    kk = is_homepage(title)
    print(kk)
